package com.flx.jvm;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author Fenglixiong
 * @Create 2021/11/30 20:45
 * @Description GC二次标记测试
 * 如果对象实现了finalize()方法那么有可能不会被回收
 **/
public class TwoGCTest {

    public static List<Object> gcList = new ArrayList<>();

    public static void main(String[] args) throws InterruptedException {

        List<Object> list = new ArrayList<>();

        int i = 0;
        int j = 0;
        while (true){
            list.add(new Student(i++));
            new Student(j--);
            Thread.sleep(10);
        }

    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class Student{

        private Integer id;

        @Override
        protected void finalize() throws Throwable {
            System.out.println("User即将被回收："+id);
            TwoGCTest.gcList.add(this);//GC之前进行重新引用
        }
    }

}
